<template>
  <div class="login-container">
    <particles-js ref="particlesJs" class="lizi" />
    <div class="urlArrDiv">
      <!-- 配置接口地址  开发展示 -->
      <el-select
        v-if="environment === 'development'"
        placeholder="接口地址"
        :value="url"
        @change="changeAxiosUrl"
      >
        <el-option
          v-for="(item, index) in baseUrlArr"
          :key="index"
          :label="item.label"
          :value="item"
        />
      </el-select>
    </div>
    <div class="loginBox">
      <div class="loginBoxCont">
        <h4 class="login_title">欢迎登录</h4>
        <el-form
          ref="loginForm"
          class="loginForm"
          :model="loginForm"
          :rules="rules"
          label-width="0"
          size="mini"
        >
          <el-form-item prop="account">
            <el-input
              v-model="loginForm.com"
              prefix-icon="el-icon-s-home"
              placeholder
            />
          </el-form-item>
          <el-form-item prop="account">
            <el-input
              v-model="loginForm.account"
              prefix-icon="el-icon-user-solid"
              placeholder="账号"
              @blur="blurChange"
            />
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              prefix-icon="el-icon-lock"
              type="password"
              show-password
              placeholder="密码"
              @keyup.enter.native="submitLogin"
            />
          </el-form-item>
        </el-form>
        <el-row class="login_submit_box">
          <el-button
            type="primary"
            class="login_submit"
            :loading="loading"
            @click="submitLogin"
            >登录</el-button
          >
        </el-row>
      </div>
    </div>
  </div>
</template>
<script>
import particlesJs from "@/components/ParticlesJs";
export default {
  components: { particlesJs },
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!this.loginForm.account) {
        callback(new Error("用户名不能为空！"));
      } else {
        callback();
      }
    };
    const validatePassword = (rule, value, callback) => {
      if (!this.loginForm.password) {
        callback(new Error("密码不能为空！"));
      } else {
        callback();
      }
    };
    return {
      loginForm: {
        com: "",
        account: "kfc",
        password: "123456",
        grantType: "password",
      },
      rules: {
        account: [
          { required: true, trigger: "blur", validator: validateUsername },
        ],
        password: [
          { required: true, trigger: "blur", validator: validatePassword },
        ],
      },
      loading: false,
      baseUrlArr: [
        {
          value: "http://172.16.2.22:8001",
          label: "dmf:22",
          username: "kfc",
          password: "123456",
        },
        {
          value: "http://172.16.1.180:8001",
          label: "xyr:180",
          username: "kfc",
          password: "123456",
        },
      ],
      environment: process.env.NODE_ENV,
      url: "",
    };
  },
  computed: {},
  created() {},
  mounted() {},
  methods: {
    async blurChange() {
      const res = await this.$axios.get(
        `/dobo-auth/comp?account=${this.loginForm.account}`
      );
      if (res.data.code === 200) {
        this.loginForm.com = res.data.data;
      }
    },
    submitLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$store
            .dispatch("user/login", this.loginForm)
            .then(async (res) => {
              // this.$notify.success('登陆成功！')
              this.$router.push(
                res.adminType === 2 ? "/companyInformation" : "/"
              );
              this.loading = false;
            })
            .catch(() => {
              this.loading = false;
            });
        } else {
          return false;
        }
      });
    },
    changeAxiosUrl(row) {
      const { username, password } = row;
      this.loginForm.account = username;
      this.loginForm.password = password;
      this.$axios.defaults.baseURL = row.value;
      sessionStorage.setItem("baseURL", row.value);
    },
  },
};
</script>

<style lang="scss" scoped>
.login-container {
  width: 100%;
  height: 100%;
  overflow: hidden;
  position: relative;
}
.urlArrDiv {
  width: 100px;
  height: 50px;
  position: absolute;
  top: 20%;
  left: 50%;
  .el-select {
    visibility: hidden;
  }

  &:hover {
    .el-select {
      visibility: visible;
    }
  }
}
.lizi {
  position: absolute;
  left: 0;
  top: 0;
}
.loginBox {
  background: url(../../assets/images/qiu.png) no-repeat center;
  background-size: 100% 100%;
  width: 996px;
  height: 550px;
  min-height: 450px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 999;
  border-radius: 7px;
  padding-top: 70px;
  .loginBoxCont {
    width: 500px;
    float: right;
  }
  .login_title {
    text-align: center;
    color: transparent;
    font-size: 30px;
    font-weight: normal;
    margin: 30px 20px 0 20px;
    -webkit-background-clip: text;
    background-image: linear-gradient(to left top, #6dc5f8, #ffffff);
    text-shadow: 1px 1px 1px rgba(0, 0, 0, 0.2);
  }
  .login_submit_box {
    margin-top: 30px;
    padding: 0 60px;
    button {
      border-radius: 20px;
      width: 100%;
      height: 40px;
      color: #d2eeff;
      background: linear-gradient(360deg, #26739b, #114e6e);
      font-size: 18px;
      border: none;
      transition: all 0.5s;
      &:hover {
        filter: drop-shadow(0 0 20px #26739b);
        transition: all 0.5s;
      }
    }
    .el-button.is-loading:before {
      background: none;
    }
  }
}
</style>

<style lang="scss">
.loginForm {
  padding: 40px 60px 0 60px;
  .el-input {
    .el-input__prefix {
      font-size: 20px;
      padding-left: 14px;
      color: #52b6f0;
    }
    .el-input__inner {
      background: none;
      background-color: rgba(255, 255, 255, 0.2) !important;
      border-radius: 50px;
      border: 1px solid #4babe3;
      padding-left: 50px;
      color: #cacaca !important;
      font-size: 14px;
      height: 45px;
    }
  }
}
</style>
